import tkinter
# 关于页面内容
from tkinter import ttk

from db import db




class AboutFrame(tkinter.Frame):
    def __init__(self,root):
        super().__init__(root)
        tkinter.Label(self,text='关于本作品，作者:碧蓝幻想').pack()
# 修改页面内容
class ChangeFrame(tkinter.Frame):
    def __init__(self,root):
        super().__init__(root)

        self.username = tkinter.StringVar()
        self.password = tkinter.StringVar()
        self.status = tkinter.StringVar()
        self.create_page()

    def create_page(self):
        tkinter.Label(self).grid(row=0,pady=10)
        # 标题和输入框
        tkinter.Label(self,text='姓名：').grid(row=1,column=1,pady=10)
        tkinter.Entry(self, textvariable=self.username).grid(row=1, column=2, pady=10)
        tkinter.Label(self, text='密码：').grid(row=2, column=1, pady=10)
        tkinter.Entry(self, textvariable=self.password).grid(row=2, column=2, pady=10)
        # 按钮和消息提示
        tkinter.Button(self,text='查询',command=self.search_user).grid(row=3,column=1,pady=10)
        tkinter.Button(self, text='修改', command=self.change_info).grid(row=3, column=2, pady=10)
        tkinter.Label(self, textvariable=self.status).grid(row=4, column=2, pady=10)
    # 查询单个数据
    def search_user(self):
        username = self.username.get()
        student=db.one(username)
        self.password.set(student[2])
        self.status.set('查询成功')
    # 修改数据
    def change_info(self):
        stu = {'username': self.username.get(), 'password': self.password.get()}
        db.update(stu)
        self.username.set('')
        self.password.set('')
        self.status.set('操作成功')
# 录入页面内容
class InsertFrame(tkinter.Frame):
    def __init__(self,root):
        super().__init__(root)

        self.username=tkinter.StringVar()
        self.password = tkinter.StringVar()
        self.status = tkinter.StringVar()
        self.create_page()

    def create_page(self):
        tkinter.Label(self).grid(row=0,pady=10)
        # 标题和输入框
        tkinter.Label(self,text='姓名：').grid(row=1,column=1,pady=10)
        tkinter.Entry(self, textvariable=self.username).grid(row=1, column=2, pady=10)
        tkinter.Label(self, text='密码：').grid(row=2, column=1, pady=10)
        tkinter.Entry(self, textvariable=self.password).grid(row=2, column=2, pady=10)

        # 按钮和消息提示
        tkinter.Button(self,text='录入',command=self.recode_info).grid(row=3,column=2,pady=10)
        tkinter.Label(self, textvariable=self.status).grid(row=4, column=2, pady=10)
    # 录入数据
    def recode_info(self):
        stu={'username':self.username.get(),'password':self.password.get()} # 字典
        self.username.set('')
        self.password.set('')
        db.insert(stu)
        self.status.set('操作成功')
# 查询页面内容
class SearchFrame(tkinter.Frame):
    def __init__(self,root):
        super().__init__(root)
        self.table_view=tkinter.Frame()
        self.table_view.pack()
        self.create_page()

    def create_page(self):
        columns=("username","password")
        columns_values=("姓名","密码")
        self.tree_view=ttk.Treeview(self,show="headings",columns=columns)
        self.tree_view.column('username',width=80,anchor="center")
        self.tree_view.column('password', width=80, anchor="center")
        self.tree_view.heading('username',text="姓名")
        self.tree_view.heading('password', text="密码")
        self.tree_view.pack(fill=tkinter.BOTH,expand=True)
        self.show_data_frame()

        tkinter.Button(self,text='刷新数据',command=self.show_data_frame).pack()
    # 获取数据，遍历数据
    def show_data_frame(self):
        # 删除旧的结点
        for _ in map(self.tree_view.delete,self.tree_view.get_children('')):
            pass
        students=db.all()
        print('数据库查询')
        print(students)
        index=0
        for stu in students:
            self.tree_view.insert('',index+1,values=(stu[1],stu[2]))
# 删除页面内容
class DeleteFrame(tkinter.Frame):
    def __init__(self,root):
        super().__init__(root)
        tkinter.Label(self, text='删除页面').pack()
        self.username=tkinter.StringVar()
        self.status=tkinter.StringVar()
        tkinter.Label(self,text='根据名称删除').pack()
        tkinter.Entry(self,textvariable=self.username).pack()
        tkinter.Button(self,text='删除',command=self.delete).pack()
        tkinter.Label(self,textvariable=self.status).pack()
    def delete(self):
        username=self.username.get()
        flag=db.delete(username)
        self.username.set('')
        if flag:
            self.status.set('操作成功')
        else:
            self.status.set('数据不存在')